package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author diva
 * @date 2025/10/8 18:13
 * @ProjectName
 */
@Slf4j
@RestController
@RequestMapping(path="/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public Result page(@RequestParam(name = "name",required = false) String name,
                     @RequestParam(name = "label",required = false) String label,
                     @RequestParam(name = "remark",required = false) String remark,
                     @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
                     @RequestParam(name = "pageSize",required = false,defaultValue = "10") Integer pageSize){
        PageResult<Role> pageResult = roleService.page(name,label,remark,page,pageSize);
        log.info("查询角色列表，参数：角色名称={}，角色标签={}，角色描述={}，当前页码={}，页码大小={}",name,label,remark,page,pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result selectAll() {
        log.info("查询角色列表");
        List<Role> roleList = roleService.selectAll();
        return Result.success(roleList);
    }
    @PostMapping
    public Result addRole(@RequestBody Role role){
        log.info("新增角色，参数：{}",role);
        roleService.addRole(role);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable("id") Integer id){
        log.info("删除角色，参数：{}",id);
        roleService.deleteRole(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable("id") Integer id){
        log.info("查询角色id：{}",id);
        Role role = roleService.getRoleById(id);
        return Result.success(role);
    }
    @PutMapping("")
    public Result updateRole(@RequestBody Role role){
        log.info("更新角色，参数：{}",role);
        roleService.updateRole(role);
        return Result.success();
    }

}
